<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class GoodsBrandController extends Controller
{
    //
    public function getIndex()
    {
    	$res = DB::table('goods_brand')->orderBy('id','asc')->get();
    	$state = array(0=>'新入驻',1=>'上架',2=>'下架');
    	return view('admin/goodsbrand/brand_index',['res'=>$res,'state'=>$state]);
    }

    public function getCreate()
    {
    	return view('admin/goodsbrand/brand_create');
    }

    public function postCreate(Request $request)
    {
    	$data = $request->except('_token','picbrand');
    	$st = $request->hasFile('picbrand');
    	if($st) {
    		$profix = $request->file('picbrand')->getClientOriginalExtension();
            //随机图片名
            $name = rand(10000,99999).time();
            $request->file('picbrand')->move('./upload/',$name.'.'.$profix);
            $data['picbrand'] = '/upload/'.$name.'.'.$profix;
    	} else {
    		return back();
    	}
    	$data['addtime'] = time();
    	$res = DB::table('goods_brand')->insert($data);
    	if($res) {
    		return redirect('/admin/goodsbrand/index');
    	} else {
    		return back();
    	}
    }

    public function getEdit()
    {
    	$id = $_GET['id'];
    	$res = DB::table('goods_brand')->where('id',$id)->first();
    	return view('admin/goodsbrand/brand_edit',['res'=>$res]);
    }

    public function postUpdate(Request $request)
    {
    	$data = $request->except('_token','id');
    	$id = $request->only('id');
    	$res = DB::table('goods_brand')->where('id',$id)->update($data);
    	if($res) {
    		return redirect('/admin/goodsbrand/index');
    	} else {
    		return back();
    	}
    }

    public function getDelete()
    {
    	$id = $_GET['id'];
    	$res = DB::table('goods_brand')->where('id',$id)->delete();
    	if($res) {
    		return redirect('/admin/goodsbrand/index');
    	} else  {
    		return back();
    	}
    }
}
